#include "ALGraph.h"
void CreateALGraph(ALGraph* G) {
    int i, j, m, n, k, s1, s2;
    ArcNode* s;
    printf("请输入所创建课程图的顶点个数和边数：");
    scanf("%d %d", &i, &j);
    G->vexnum = i;
    G->arcnum = j;
    G->Adjlist = (VNode*)malloc(sizeof(VNode) * i);
    printf("\n请输入顶点的信息顺序如下:\n");
    printf("\n课程编号 课程名称 学分 周数\n");
    for (i = 0; i < G->vexnum; i++) {
        //scanf("%d %s %f %d", &G->Adjlist[i].data.num, G->Adjlist[i].data.name,
        //      &G->Adjlist[i].data.score, &G->Adjlist[i].data.weeks);
        scanf("%d", &G->Adjlist[i].data.num);
        scanf("%s", G->Adjlist[i].data.name);
        scanf("%f", &G->Adjlist[i].data.score);
        scanf("%d", &G->Adjlist[i].data.weeks);
        //检测输入编号是否重复
        int flag = 0;//用于标志课程编号是否重复，重复则为赋值为1,不重复赋值为2
        int f;
        while (1)
        {
            for (f = 0; f < i; f++)
            {
                if (G->Adjlist[i].data.num == G->Adjlist[f].data.num)
                    flag = 1;

            }
            if (flag == 1)
            {
                printf("输入课程编号与之前课程编号重复，请重新输入：\n");
                flag = 0;
                scanf_s("%d", &G->Adjlist[i].data.num);
            }
            else
            {
                //printf("输入正确，请继续输入：\n");
                break;
            }
        }
        //
        G->Adjlist[i].firstarc = '\0';
        G->Adjlist[i].data.term = 0;
        G->Adjlist[i].data.indegree = 0;
    }
    printf("\n请输入顶点关系:(课程编号(弧尾) 课程编号(弧头))\n");
    for (j = 1; j <= G->arcnum; j++) {
        scanf("%d", &s1);
        scanf("%d", &s2);
        if (s1 == s2) {
            printf("输入不合理！请重新输入：\n");
            j--;
            continue;
        }
        else {
            m = GLocateVex(G, s1);
            n = GLocateVex(G, s2);
            if (m == -1 || n == -1) {
                printf("在图中没有找到该编号，请重新输入:\n");
                j--;
                continue;
            }
            else {
                s = (ArcNode*)malloc(sizeof(ArcNode));
                if (!s) {
                    printf("储存分配失败!");
                }
                s->adjvex = n;
                s->nextarc = G->Adjlist[m].firstarc;
                G->Adjlist[m].firstarc = s;
                G->Adjlist[n].data.indegree++;
            }
        }
    }
    printf("创建成功!\n");
}
int GLocateVex(ALGraph* G, int x) {
    int i, j = -1;
    for (i = 0; i < G->vexnum; i++) {
        if (G->Adjlist[i].data.num == x) {
            j = i;
            break;
        }
    }
    return j;
} //判断图中是否有顶点x,若存在则返回x的位置，否则返回-1
void FindInDegree(ALGraph* G, int indegree[]) {
    int i;
    ArcNode* p;
    for (i = 0; i < G->vexnum; i++)
        indegree[i] = 0;
    /*
    for (i = 0; i < G->vexnum; i++) {
        p = G->Adjlist[i].firstarc;
        while (p) {
            indegree[p->adjvex]++;
            p = p->nextarc;
        }
    }
    */
    for (i = 0; i < G->vexnum; i++) {
        indegree[i] = G->Adjlist[i].data.indegree;
    }
}